<script setup lang="tsx">
import { ContentWrap } from '@/components/ContentWrap'
import { Search } from '@/components/Search'
import { Table } from '@/components/Table'
import type { DepartmentItem } from '@/api/department/types'
import { useTable } from '@/hooks/web/useTable'
import { ref, unref, reactive ,onMounted} from 'vue'
import { useRouter } from 'vue-router'
import { getDronePage,droneDelete} from '@/api/resource-manage'
import { useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { BaseButton } from '@/components/Button'
import crudSchemas, { getDataInstance, getRouterInstance,setMountType ,setDroneType} from './config/index'

onMounted(()=>{
  setDroneType()
  setMountType()
})

// 注册列表页函数
const { tableRegister, tableState, tableMethods } = useTable({
  // 列表查询
  fetchDataApi: async () => {
    const { currentPage, pageSize } = tableState
    const res = await getDronePage({
      page: unref(currentPage),
      limit: unref(pageSize),
      ...unref(searchParams)
    }) as any

    return {
      list: res.data.records,
      total: res.data.total
    }
  },
    //删除
    fetchDelApi: async (droneId:number|string) => {
    const res = await droneDelete({droneId})
    return !!res
  }
})
const { loading, dataList, total, currentPage, pageSize } = tableState

const { getList , delList} = tableMethods

const searchParams = ref({})

// 查询
const setSearchParams = (params: any) => {
  searchParams.value = params
  getList()
}


// 载入路由
const router = useRouter()
getRouterInstance(router)

// 入口解析页面config
const { allSchemas } = useCrudSchemas(crudSchemas)

// 新增
const AddAction = () => {
  router.push({ path: '/resource-manage/drone_info' })
}

const delData =async (row) => {
  console.log(row)
  loading.value = true
  await delList(unref(row.id)).finally(() => {
    loading.value = false
  })
}

// 获取data对象
getDataInstance(
  reactive({
    delData
  })
)
</script>

<template>
  <ContentWrap>
    <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />

    <div class="mb-10px">
      <BaseButton type="primary" @click="AddAction">新增 </BaseButton>
    </div>

    <Table
      v-model:pageSize="pageSize"
      v-model:currentPage="currentPage"
      :columns="allSchemas.tableColumns"
      :data="dataList"
      :loading="loading"
      :pagination="{
        total: total
      }"
      @register="tableRegister"
    />
  </ContentWrap>
</template>
